package td.pt.widget;

import td.domain.Survey;
import td.pt.ui.UsabilityScaleQuestion;
import td.pt.ui.UsabilityTextQuestion;
import td.shared.action.SurveyAction;
import td.shared.action.SurveyActionResult;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.PopupView;
import com.gwtplatform.mvp.client.PresenterWidget;

public class SurveyPresenter extends PresenterWidget<SurveyPresenter.MyView> {

	public interface MyView extends PopupView {
		public UsabilityScaleQuestion getLayout();

		public UsabilityScaleQuestion getCategoryTree();

		public Widget getWidget();

		public UsabilityScaleQuestion getTagTree();

		public UsabilityScaleQuestion getTopThoughts();

		public UsabilityScaleQuestion getGeneralNavigation();

		public UsabilityScaleQuestion getSignUpLogin();

		public UsabilityScaleQuestion getTagging();

		public UsabilityScaleQuestion getAddToActiveRefs();

		public UsabilityScaleQuestion getCreateNewThought();

		public UsabilityScaleQuestion getAddCategoryToNewThought();

		public UsabilityScaleQuestion getRefsInNewThought();

		public UsabilityScaleQuestion getPublish();

		public Integer getListing();

		public UsabilityTextQuestion getWhyListing();

		public UsabilityTextQuestion getTheBest();

		public UsabilityTextQuestion getOverall();

		public UsabilityTextQuestion getComments();

		public UsabilityTextQuestion getMostConfusing();

		public Button getSubmit();
	}

	@Inject
	DispatchAsync dispatchAsync;

	private AsyncCallback<SurveyActionResult> surveyCallback = new AsyncCallback<SurveyActionResult>() {

		@Override
		public void onSuccess(SurveyActionResult result) {
			Window.alert("Survey saved sucessfully, thank you very much!");
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Couldn't save the survey, something went wrong :(");
		}
	};

	@Inject
	public SurveyPresenter(final EventBus eventBus, final MyView view) {
		super(eventBus, view);
		getView().getLayout().setControlName("Layout");
		getView().getCategoryTree().setControlName("CategoryTree");
		getView().getTagTree().setControlName("TagTree");
		getView().getTopThoughts().setControlName("TopThoughts");
		getView().getGeneralNavigation().setControlName("GeneralNavigation");
		getView().getSignUpLogin().setControlName("SignUpLogin");
		getView().getTagging().setControlName("Tagging");
		getView().getAddToActiveRefs().setControlName("AddToActiveRefs");
		getView().getCreateNewThought().setControlName("CreateNewThought");
		getView().getAddCategoryToNewThought().setControlName(
				"AddCategoryToNewThought");
		getView().getRefsInNewThought().setControlName("RefsInNewThought");
		getView().getPublish().setControlName("Publish");

	}

	@Override
	protected void onBind() {
		super.onBind();
		getView().getSubmit().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getView().hide();
				Survey survey = new Survey();
				survey.setAddCategoryToNewThought(getView()
						.getAddCategoryToNewThought().getValue());
				survey.setAddToActiveRefs(getView().getAddToActiveRefs()
						.getValue());
				survey.setCategoryTree(getView().getCategoryTree().getValue());
				survey.setComments(getView().getComments().getValue());
				survey.setCreateNewThought(getView().getCreateNewThought()
						.getValue());
				survey.setGeneralNavigation(getView().getGeneralNavigation()
						.getValue());
				survey.setLayout(getView().getLayout().getValue());
				survey.setListing(getView().getListing());
				survey.setMostConfusing(getView().getMostConfusing().getValue());
				survey.setOverall(getView().getOverall().getValue());
				survey.setPublish(getView().getPublish().getValue());
				survey.setRefsInNewThought(getView().getRefsInNewThought()
						.getValue());
				survey.setSignUpLogin(getView().getSignUpLogin().getValue());
				survey.setTagging(getView().getTagging().getValue());
				survey.setTagTree(getView().getTagTree().getValue());
				survey.setTheBest(getView().getTheBest().getValue());
				survey.setTopThoughts(getView().getTopThoughts().getValue());
				survey.setWhyListing(getView().getWhyListing().getValue());

				SurveyAction surveyAction = new SurveyAction(survey);
				dispatchAsync.execute(surveyAction, surveyCallback);
			}
		});
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}
}
